//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.ligson.springbootrestdao.event;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.hibernate.event.spi.*;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.proxy.HibernateProxy;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class CruxEventListener implements PreCollectionRecreateEventListener, PostCollectionRecreateEventListener, PostUpdateEventListener, PreUpdateEventListener, PreInsertEventListener, PostInsertEventListener, PostDeleteEventListener, PreDeleteEventListener {
    private ApplicationContext applicationContext;

    public CruxEventListener(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public void onPostUpdate(PostUpdateEvent event) {
        //if (event.getEntity() instanceof BaseEntity) {
        Object obj = event.getEntity();
        int[] index = event.getDirtyProperties();
        List<ModifyProperty> oldProperties = new ArrayList();
        List<ModifyProperty> properties = new ArrayList();
        if (ArrayUtils.isNotEmpty(index)) {
            int[] var6 = index;
            int var7 = index.length;

            for (int var8 = 0; var8 < var7; ++var8) {
                int i = var6[var8];
                ModifyProperty property;
                if (this.isRetain(event.getOldState()[i])) {
                    property = new ModifyProperty();
                    property.setName(event.getPersister().getPropertyNames()[i]);
                    property.setValue(event.getOldState()[i]);
                    if (event.getOldState()[i] != null) {
                        property.setType(event.getOldState()[i].getClass().getSimpleName());
                    }

                    oldProperties.add(property);
                }

                if (this.isRetain(event.getState()[i])) {
                    property = new ModifyProperty();
                    property.setName(event.getPersister().getPropertyNames()[i]);
                    property.setValue(event.getState()[i]);
                    if (event.getState()[i] != null) {
                        property.setType(event.getState()[i].getClass().getSimpleName());
                    }

                    properties.add(property);
                }
            }
        }

        EntityModifyEvent entityModifyEvent = new EntityModifyEvent(this, obj.getClass(), obj, (String) event.getId(), EntityModifyEvent.ModifyType.UPDATE, oldProperties, properties);
        this.applicationContext.publishEvent(entityModifyEvent);
        //}

    }

    public boolean requiresPostCommitHanding(EntityPersister entityPersister) {
        return true;
    }

    public boolean requiresPostCommitHandling(EntityPersister entityPersister) {
        return true;
    }

    public boolean onPreUpdate(PreUpdateEvent event) {
        //if (event.getEntity() instanceof BaseEntity) {
        Object obj = event.getEntity();
        List<ModifyProperty> oldProperties = new ArrayList();
        List<ModifyProperty> properties = new ArrayList();
        int i;
        ModifyProperty property;
        if (event.getOldState() != null) {
            for (i = 0; i < event.getOldState().length; ++i) {
                if (this.isRetain(event.getOldState()[i])) {
                    property = new ModifyProperty();
                    property.setName(event.getPersister().getPropertyNames()[i]);
                    property.setValue(event.getOldState()[i]);
                    if (event.getOldState()[i] != null) {
                        property.setType(event.getOldState()[i].getClass().getSimpleName());
                    }

                    oldProperties.add(property);
                }
            }
        }

        if (event.getState() != null) {
            for (i = 0; i < event.getState().length; ++i) {
                if (this.isRetain(event.getState()[i])) {
                    property = new ModifyProperty();
                    property.setName(event.getPersister().getPropertyNames()[i]);
                    property.setValue(event.getState()[i]);
                    if (event.getState()[i] != null) {
                        property.setType(event.getState()[i].getClass().getSimpleName());
                    }

                    properties.add(property);
                }
            }
        }

        EntityModifyEvent entityModifyEvent = new EntityModifyEvent(this, obj.getClass(), obj, (String) event.getId(), EntityModifyEvent.ModifyType.PRE_UPDATE, oldProperties, properties);
        this.applicationContext.publishEvent(entityModifyEvent);
        // }

        return false;
    }

    public boolean onPreInsert(PreInsertEvent event) {
        //if (event.getEntity() instanceof BaseEntity) {
        Object object = event.getEntity();
        List<ModifyProperty> properties = new ArrayList();
        if (event.getState() != null) {
            for (int i = 0; i < event.getState().length; ++i) {
                if (this.isRetain(event.getState()[i])) {
                    ModifyProperty property = new ModifyProperty();
                    property.setName(event.getPersister().getPropertyNames()[i]);
                    property.setValue(event.getState()[i]);
                    if (event.getState()[i] != null) {
                        property.setType(event.getState()[i].getClass().getSimpleName());
                    }

                    properties.add(property);
                }
            }
        }

        EntityModifyEvent entityModifyEvent = new EntityModifyEvent(this, object.getClass(), object, (String) event.getId(), EntityModifyEvent.ModifyType.PRE_NEW, new ArrayList(), properties);
        this.applicationContext.publishEvent(entityModifyEvent);
        //}

        return false;
    }

    public void onPostInsert(PostInsertEvent event) {
        //if (event.getEntity() instanceof BaseEntity) {
        Object object = event.getEntity();
        List<ModifyProperty> properties = new ArrayList();
        if (event.getState() != null) {
            for (int i = 0; i < event.getState().length; ++i) {
                if (this.isRetain(event.getState()[i])) {
                    ModifyProperty property = new ModifyProperty();
                    property.setName(event.getPersister().getPropertyNames()[i]);
                    property.setValue(event.getState()[i]);
                    if (event.getState()[i] != null) {
                        property.setType(event.getState()[i].getClass().getSimpleName());
                    }

                    properties.add(property);
                }
            }
        }

        EntityModifyEvent entityModifyEvent = new EntityModifyEvent(this, object.getClass(), object, (String) event.getId(), EntityModifyEvent.ModifyType.NEW, new ArrayList(), properties);
        this.applicationContext.publishEvent(entityModifyEvent);
        //}

    }

    public void onPostDelete(PostDeleteEvent event) {
        // if (event.getEntity() instanceof BaseEntity) {
        Object object = event.getEntity();
        EntityModifyEvent entityModifyEvent = new EntityModifyEvent(this, object.getClass(), object, (String) event.getId(), EntityModifyEvent.ModifyType.DELETE, (List) null, (List) null);
        this.applicationContext.publishEvent(entityModifyEvent);
        //}

    }

    public boolean onPreDelete(PreDeleteEvent event) {
        // if (event.getEntity() instanceof BaseEntity) {
        Object object = event.getEntity();
        EntityModifyEvent entityModifyEvent = new EntityModifyEvent(this, object.getClass(), object, (String) event.getId(), EntityModifyEvent.ModifyType.PRE_DELETE, (List) null, (List) null);
        this.applicationContext.publishEvent(entityModifyEvent);
        //}

        return false;
    }

    private boolean isRetain(Object object) {
        return !(object instanceof HibernateProxy);
    }

    @Override
    public void onPreRecreateCollection(PreCollectionRecreateEvent event) {
        log.debug("collection............");
    }

    @Override
    public void onPostRecreateCollection(PostCollectionRecreateEvent event) {
        log.debug("collection............");
    }
}
